"""小改动了一下许利恒的不变量
"""

import numpy as np
from .base import BinadPlainBuilder
from utils.ellipse import center_ellipse


class ChenBuilder(BinadPlainBuilder):
    """
    小改动不变量构建方式的二元组不变量描述子库
    """

    def invariant(self, C1, C2):
        """
        本函数需要并行化计算
        """
        assert C1.shape[1] == 3 and C2.shape[1] == 3
        cnt1 = np.pad(
            center_ellipse(C1), ((0, 1), (0, 0)), "constant", constant_values=1
        )
        cnt2 = np.pad(
            center_ellipse(C2), ((0, 1), (0, 0)), "constant", constant_values=1
        )

        I1 = np.einsum("in,nij,jn->n", cnt1, C2, cnt1) / np.einsum(
            "in,nij,jn->n", cnt2, C2, cnt2
        )
        I2 = np.einsum("in,nij,jn->n", cnt2, C1, cnt2) / np.einsum(
            "in,nij,jn->n", cnt1, C1, cnt1
        )
        return I1, I2
